package cn.sunzn.reflectcase;

import java.lang.reflect.Method;

public class GetMethod {
   public static void main(String[] args) {
       getMethodInfo("cn.sunzn.reflectcase.TestMethod","getResult",3);
   }

   /**
    * 传入全类名获得对应类中所有方法名和参数名
    */
   @SuppressWarnings("rawtypes")
   private static void getMethodInfo(String pkgName,String methodNameS,int flagPosi) {
       try {
           Class clazz = Class.forName(pkgName);
           Method[] methods = clazz.getMethods();
           int flag=1;
    	  for (Method method : methods) {
               String methodName = method.getName();
               if(methodNameS.equals(methodName)){
            	   if(flag==flagPosi){
            		   System.out.println("方法名称:" + methodName);
                       Class<?>[] parameterTypes = method.getParameterTypes();
                       for (Class<?> clas : parameterTypes) {
                           String parameterName = clas.getName();
                           System.out.println("参数名称:" + parameterName);
                       } 
                       break;
            	   }else{
            		   flag++;
            	   }
            	   
               }
               System.out.println();
           }
       } catch (ClassNotFoundException e) {
           e.printStackTrace();
       }
   }
}